package com.link2room.optimus.controller.manager;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.optimus.dto.param.base.TaobaoXrateplanBaseParamDto;
import com.link2room.optimus.dto.result.base.TaobaoXrateplanDto;
import com.link2room.optimus.facade.ITaobaoXrateplanFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.link2room.optimus.constant.PermissionConstant.*;

/**
 * Created by VegetaKo on 2017/1/12.
 */
@Api(tags = "淘宝价格计划管理")
@RestController
@RequestMapping("/manager/TaobaoXrateplans")
public class TaobaoXrateplanManagerController {

    @Resource
    private ITaobaoXrateplanFacadeService taobaoXrateplanFacadeService;

    @ApiOperation(value = "获取淘宝价格计划列表",response = TaobaoXrateplanDto.class)
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = taobaoXrateplanList, name = "淘宝价格计划列表")
    public BaseResultDto taobaoXrateplanList(
            @RequestParam(value = "mcCode" ,required = false) String mcCode,
            @RequestParam(value = "currentPage" ,required = false) Integer currentPage,
            @RequestParam(value = "pageSize" ,required = false) Integer pageSize
    ) throws Exception {
        TaobaoXrateplanBaseParamDto taobaoXrateplanBaseParamDto =new TaobaoXrateplanBaseParamDto();
        taobaoXrateplanBaseParamDto.setMcCode(mcCode);
        taobaoXrateplanBaseParamDto.setCurrentPage(currentPage);
        taobaoXrateplanBaseParamDto.setPageSize(pageSize);
        QueryResult<TaobaoXrateplanDto> taobaoXrateplanResult = taobaoXrateplanFacadeService.listTaobaoXrateplans(taobaoXrateplanBaseParamDto);
        BaseResultDto<TaobaoXrateplanDto> baseResultDto=new BaseResultDto<>();
        baseResultDto.setResultCode(0);
        baseResultDto.setQueryResult(taobaoXrateplanResult);
        return baseResultDto;
    }

    @ApiOperation(value = "根据ID获取淘宝价格计划",response = TaobaoXrateplanDto.class)
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @L2RPermissionResources(value = taobaoXrateplanById, name = "ID获取淘宝价格计划")
    public BaseResultDto taobaoXrateplan(
            @PathVariable Long id
    ) throws Exception {
        TaobaoXrateplanBaseParamDto taobaoXrateplanBaseParamDto =new TaobaoXrateplanBaseParamDto();
        taobaoXrateplanBaseParamDto.setId(id);
        TaobaoXrateplanDto taobaoXrateplanDto = taobaoXrateplanFacadeService.findTaobaoXrateplanById(taobaoXrateplanBaseParamDto);
        BaseResultDto<TaobaoXrateplanDto> baseResultDto=new BaseResultDto<>();
        baseResultDto.setResultCode(0);
        baseResultDto.setResultInfo(taobaoXrateplanDto);
        return baseResultDto;
    }

    @ApiOperation(value = "根据rpCode获取淘宝价格计划",response = TaobaoXrateplanDto.class)
    @RequestMapping(value = "/TaobaoXrateplan",method = RequestMethod.GET)
    @L2RPermissionResources(value = taobaoXrateplan, name="淘宝价格计划详情")
    public BaseResultDto taobaoXrateplan(
            @RequestParam(value = "rpCode" ,required = false) String rpCode
    ) throws Exception {
        TaobaoXrateplanBaseParamDto taobaoXrateplanBaseParamDto =new TaobaoXrateplanBaseParamDto();
        taobaoXrateplanBaseParamDto.setRpCode(rpCode);
        TaobaoXrateplanDto taobaoXrateplanDto = taobaoXrateplanFacadeService.findTaobaoXrateplanByRpcode(taobaoXrateplanBaseParamDto);
        BaseResultDto<TaobaoXrateplanDto> baseResultDto=new BaseResultDto<>();
        baseResultDto.setResultCode(0);
        baseResultDto.setResultInfo(taobaoXrateplanDto);
        return baseResultDto;
    }

    @ApiOperation(value = "更新淘宝价格计划")
    @RequestMapping(value = "/TaobaoXrateplan",method= RequestMethod.PUT)
    @L2RPermissionResources(value = updateTaobaoXrateplan, name="更新淘宝价格计划")
    public BaseResultDto updateTaobaoXrateplan(
            @RequestBody TaobaoXrateplanBaseParamDto taobaoXrateplanBaseParamDto
    )throws Exception{
        TaobaoXrateplanDto taobaoXrateplanDto = taobaoXrateplanFacadeService.saveOrUpdateTaobaoXrateplan(taobaoXrateplanBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(taobaoXrateplanDto);
        return l2rRs;
    }
}
